<?php
/**
 * 分页抽象类
 *
 * @author iwind <iwind.iwind@gmail.com>
 * @link http://www.4kiki.net/
 * @version $Id: IAbstractPager.php 327 2008-05-20 16:57:38Z iwind $
 * @package net.4kiki.pager
 */

/**
 * 分页抽象类
 * 
 * 实现了IPager接口的一些方法
 *
 * @version $Id: IAbstractPager.php 327 2008-05-20 16:57:38Z iwind $
 * @package net.4kiki.pager
 */
abstract class IAbstractPager extends IMagicObject implements IPager {
	private $keyword;
	private $total;
	private $path;
	private $size;
	private $properties;
	private $query;
	private $length;
	private $rows = array();
	/**
	 * 是否加载了本地化对象
	 *
	 * @var boolean
	 * @since 0.2.2
	 */
	private $localeLoaded = false;
	
	/**
	 * 语言配置
	 *
	 * @var array
	 * @since 0.2.2
	 */
	private $messages = array();
	
	/**
	 * 加载配置
	 * 
	 * 示例：
	 * <pre>
	 *		$pager = new IYahooPager();
	 *		$pager->load(array(
	 *			"total" => IDAO::query("TLog")->count(),
	 *			"keyword" => "page",
	 *			"size" => 10
	 *		));
	 *	</pre>
	 *
	 * @param array $config 配置
	 * @since 0.1
	 */
	public function load(array $config) {
		foreach ($config as $key=>$value) {
			IReflection::setProperty($this, $key, $value);
		}
	}
	
	public function current() {
		$request = IRequest::singleton();
		$keyword = $this->keyword();
		$pageNo = intval($request->param($keyword));
		if ($pageNo <= 0) {
			$pageNo = 1;
		}
		return min($pageNo, $this->length());
	}
	
	public function offset() {
		$offset = $this->size * ($this->current() - 1);
		if ($offset < 0) {
			$offset = 0;
		}
		return $offset;
	}
	
	public function setTotal($total) {
		$this->total =  intval($total);
		if ($this->total < 0) {
			throw new IException("content total '{$total}' can't be small than 0");
		}
	}
	
	public function total() {
		return $this->total;
	}
	
	public function setKeyword($keyword) {
		$this->keyword = $keyword;
	}
	/**
	 * 取得分页用的关键字
	 *
	 * 从0.1开始，如果没有关键字，则默认为page
	 * 
	 * @return string
	 */
	public function keyword() {
		if ($this->keyword == null) {
			$this->keyword = "page";
		}
		return $this->keyword;
	}
	
	public function setSize($size) {
		$this->size = intval($size);
		if ($this->size < 1) {
			throw new IException("page size '{$size}' can't be small than 1");
		}
	}
	
	public function size() {
		return $this->size;
	}
	
	public function setPath($path) {
		$this->path = $path;
	}
	
	public function path() {
		return $this->path;
	}
	
	public function setProperties(array $properties) {
		$this->properties = $properties;
	}
	
	public function properties() {
		return $this->properties;
	}
	
	public function setQuery($query) {
		if (is_array($query)) {
			$_query = array();
			foreach ($query as $key=>$value) {
				$_query [] = "{$key}=" . $value;
			}
			$query = implode("&", $_query);
		}
		$this->query = $query;
	}
	
	public function query() {
		return $this->query;
	}
	
	public function url($pageNo) {
		$query = $this->query();
		if (strstr($query, IPager::PAGER_VARIABLE_STRING)) {
			$query = str_replace(IPager::PAGER_VARIABLE_STRING, $pageNo, $query);
		}
		else {
			if ($query == "") {
				$query = $this->keyword() . "=" . $pageNo;
			}
			else {
				$query .= "&" . $this->keyword() . "=" . $pageNo;
			}
		}
		return $this->path() . "?" . $query;
	}
	
	public function length() {
		return ceil($this->total()/$this->size());
	}
	
	public function addRow($row) {
		$this->rows[] = $row;
	}
	
	public function addRows(array $rows) {
		foreach ($rows as $row) {
			$this->rows[] = $row;
		}
	}
	
	public function rows() {
		return $this->rows;
	}
	
	public function setRows($rows) {
		if (!is_array($rows) && (!is_object($rows) || !isInstanceOf($rows, "IteratorAggregate") || !isInstanceOf($rows, "ArrayAccess"))) {
			return;
		}
		$this->rows = $rows;
	}
	
	/**
	 * 取得键值对应的消息文本
	 *
	 * @param string $key 键值
	 * @return string
	 * @since 0.2.1
	 */
	protected function message($key) {
		if (!$this->localeLoaded) {
			$app = IApplication::singleton();
			$locale = $app->locale()->name();
			if ($locale == null) {
				$locale = "zh_CN";
			}
			
			//简写
			$dirname = dirname(__FILE__) . "/lang";
			$langFile = $dirname . "/" . $locale . ".php";
			if (is_file($langFile)) {
				require($langFile);		
				$this->messages = $message;	
			}
			$this->localeLoaded = true;
		}
		if (is_array($this->messages) && array_key_exists($key, $this->messages)) {
			return $this->messages[$key];
		}
		return null;
	}
}


?>